package com.wjh.anyview.controller;

import com.wjh.anyview.entity.bo.courseServiceBo.InstitutionGradeBo;
import com.wjh.anyview.entity.bo.examServiceBo.*;
import com.wjh.anyview.entity.dto.CommonResult;
import com.wjh.anyview.entity.po.Bank;
import com.wjh.anyview.entity.po.Question;
import com.wjh.anyview.entity.vo.examControllerVo.BankVo;
import com.wjh.anyview.entity.vo.examControllerVo.ExamTeacherVo;
import com.wjh.anyview.entity.vo.examControllerVo.ExamVo;
import com.wjh.anyview.service.BankService;
import com.wjh.anyview.service.ExamService;
import com.wjh.anyview.service.QuestionService;
import com.wjh.anyview.service.constant.StringConstant;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wjh
 * @date 2021/10/9 18:41
 * @Package com.wjh.anyview.controller
 */
@RestController
@RequestMapping("/api/examination")
public class ExaminationController {
    @Resource
    private ExamService examService;

    @Resource
    private QuestionService questionService;

    @Resource
    private BankService bankService;

    @PostMapping("/listExamByClazzId")
    @PreAuthorize("hasAuthority('/listExamByClazzId')")
    public CommonResult<List<ExamVo>> listExamByClazzId(@RequestParam Long clazzId) {
        return examService.listExamByClazzId(clazzId);
    }

    @RequestMapping("/listQuestionByExamId")
    @PreAuthorize("hasAuthority('/listQuestionByExamId')")
    public CommonResult<List<Question>> listQuestionByExamId(@RequestParam("examId") Long examId) {
        return questionService.listQuestionByByExamId(examId);
    }

    @RequestMapping("/finish")
    @PreAuthorize("hasAuthority('/finish')")
    public CommonResult<?> finish(@RequestBody FinishBo finishBo) {
        return examService.finish(finishBo);
    }

    @RequestMapping("/listBankByInstitutionGrade")
    @PreAuthorize("hasAuthority('/listBankByInstitutionGrade')")
    public CommonResult<List<BankVo>> listBankByInstitutionGrade(@RequestBody InstitutionGradeBo institutionGradeBo) {
        return bankService.listBankByInstitutionGrade(institutionGradeBo);
    }

    @RequestMapping("/arrangeExamByCourseId")
    @PreAuthorize("hasAuthority('/arrangeExamByCourseId')")
    public CommonResult<?> arrangeExamByCourseId(@RequestBody ArrangeExamBo arrangeExamBo) {
        return examService.arrangeExamByCourseId(arrangeExamBo);
    }

    @RequestMapping("/getExamByCourseId")
    @PreAuthorize("hasAuthority('/getExamByCourseId')")
    public CommonResult<ExamVo> getExamByCourseId(@RequestParam("courseId") Long courseId) {
        return examService.getExamByCourseId(courseId);
    }

    @RequestMapping("/listExamByInvigilatorId")
    @PreAuthorize("hasAuthority('/listExamByInvigilatorId')")
    public CommonResult<List<ExamTeacherVo>> listExamByInvigilatorId(@RequestParam Long invigilatorId) {
        return examService.listExamByInvigilatorId(invigilatorId);
    }

    @RequestMapping("/beginExam")
    @PreAuthorize("hasAuthority('/beginExam')")
    public CommonResult<List<String>> beginExam(@RequestBody BeginExamBo beginExamBo) {
        return examService.beginExam(beginExamBo);
    }

    @RequestMapping("/submitAnswer")
    @PreAuthorize("hasAuthority('/submitAnswer')")
    public CommonResult<?> submitAnswer(@RequestBody SubmitAnswerBo submitAnswerBo) {
        return examService.submitAnswer(submitAnswerBo);
    }

    @RequestMapping("/queryAnswer")
    @PreAuthorize("hasAuthority('/queryAnswer')")
    public CommonResult<List<String>> queryAnswer(@RequestBody QueryAnswerBo queryAnswerBo) {
        return examService.queryAnswer(queryAnswerBo);
    }

    @RequestMapping("/listQuestionByBankId")
    @PreAuthorize("hasAuthority('/listQuestionByBankId')")
    public CommonResult<List<Question>> listQuestionByBankId(@RequestParam("bankId") Long bankId) {
        return questionService.listQuestionByBankId(bankId);
    }

    @RequestMapping("/addQuestionByBankId")
    @PreAuthorize("hasAuthority('/addQuestionByBankId')")
    public CommonResult<?> addQuestionByBankId(@RequestBody NewQuestionBo newQuestionBo) {
        return questionService.addQuestionByBankId(newQuestionBo);
    }

    @RequestMapping("/saveBank")
    @PreAuthorize("hasAuthority('/saveBank')")
    public CommonResult<?> saveBank(@RequestBody Bank bank) {
        return bankService.saveBank(bank);
    }

    @RequestMapping("/listNumByExamId")
    @PreAuthorize("hasAuthority('/listNumByExamId')")
    public CommonResult<List<String>> listNumByExamId(@RequestParam("examId") Long examId) {
        List<String> list = examService.listNumByExamId(examId);
        if (list.size() > 0) {
            return CommonResult.operateSuccess(list);
        }
        return CommonResult.operateFailWithMessage(StringConstant.SYSTEM_BUSY);
    }

    @RequestMapping("/listStudentAnswerByNumAndExamId")
    @PreAuthorize("hasAuthority('/listStudentAnswerByNumAndExamId')")
    public CommonResult<List<Question>> listStudentAnswerByNumAndExamId(@RequestBody NumAndExamIdBo numAndExamIdBo) {
        return examService.listStudentAnswerByNumAndExamId(numAndExamIdBo);
    }

    @PostMapping("/mark")
    @PreAuthorize("hasAuthority('/mark')")
    public CommonResult<?> mark(@RequestBody MarkBo markBo) {
        return examService.mark(markBo);
    }
}
